package com.tm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.tm.mapper.deptMapper;
import com.tm.model.Dept;
import com.tm.model.Person;
import com.tm.service.deptService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author huangp
 * @create 2021-09-12 11:25
 */
@Service
public class deptServiceImpl implements deptService {

    @Resource
    deptMapper deptMapper;
    @Override
    public List<Map<String, Object>> allDept() {
        List<Dept> list=deptMapper.selectList(null);
        List<Map<String, Object>> childrenList = childrenDept(0, list);
        return childrenList;
    }
    public List<Map<String, Object>> childrenDept(Integer pid,List<Dept> list){
        List<Map<String, Object>> childrenList  =new ArrayList<>();
        list.forEach(a->{
            if(a.getPid()==pid){
                Map<String, Object> map = new HashMap<>();
                map.put("label",a.getName());
                List<Map<String, Object>> children  = childrenDept(a.getId(),list);
                map.put("id",a.getId());
                //isEmpty判断是否为空
                if(!children.isEmpty()){
                    map.put("children",children);
                }
                childrenList.add(map);
            }
        });
        return childrenList;
    }

    @Override
    public void addDept(Dept dept) {
        Date data = new Date();
        dept.setCreateDate(data);
        dept.setUpdateDate(data);
        deptMapper.insert(dept);
    }

    @Override
    public Dept queryDeptById(Integer id) {

        return deptMapper.selectById(id);
    }

    @Override
    public void exportFile(HttpServletResponse response) {
        List<Dept> depts = deptMapper.selectList(null);
        String fileName = "C:\\Users\\Administrator\\Desktop\\部门信息.xlsx";
        EasyExcel.write(fileName, Dept.class).sheet().doWrite(depts);
    }
    @Override
    public void uploadFile(MultipartFile dataFile) {
        ArrayList<Dept> depts = new ArrayList<>();
        try {
            InputStream inputStream = dataFile.getInputStream();
            EasyExcel.read(inputStream, Dept.class, new AnalysisEventListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                    depts.add((Dept)o);
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    System.out.println("读取成功");
                }
            }).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
        depts.forEach(dept -> {
            deptMapper.insert(dept);
        });
    }

    @Override
    public void updateData(Dept dept) {
            DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            Date data = new Date();
            dept.setUpdateDate(data);
            deptMapper.updateById(dept);

    }

    @Override
    public void delDeptById(Integer id) {
        deptMapper.deleteById(id);
    }



}
